package com.ruoyi.media.serverConfig.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.media.serverConfig.domain.bo.MediaServerConfigBo;
import com.ruoyi.media.serverConfig.domain.vo.MediaServerConfigVo;
import com.ruoyi.media.serverConfig.domain.MediaServerConfig;
import com.ruoyi.media.serverConfig.mapper.MediaServerConfigMapper;
import com.ruoyi.media.serverConfig.service.IMediaServerConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 服务器配置Service业务层处理
 *
 * @author dqw
 * @date 2023-03-23
 */
@RequiredArgsConstructor
@Service
public class MediaServerConfigServiceImpl implements IMediaServerConfigService {

    private final MediaServerConfigMapper baseMapper;

    /**
     * 查询服务器配置
     */
    @Override
    public MediaServerConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务器配置列表
     */
    @Override
    public TableDataInfo<MediaServerConfigVo> queryPageList(MediaServerConfigBo bo, PageQuery pageQuery) {
        bo.setDelFlag(0);
        LambdaQueryWrapper<MediaServerConfig> lqw = buildQueryWrapper(bo);
        Page<MediaServerConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务器配置列表
     */
    @Override
    public List<MediaServerConfigVo> queryList(MediaServerConfigBo bo) {
        LambdaQueryWrapper<MediaServerConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MediaServerConfig> buildQueryWrapper(MediaServerConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MediaServerConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, MediaServerConfig::getDeptId, bo.getDeptId());
        lqw.eq(bo.getDelFlag() != null, MediaServerConfig::getDelFlag, bo.getDelFlag());
        lqw.eq(bo.getUserId() != null, MediaServerConfig::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getExpirationTime()), MediaServerConfig::getExpirationTime, bo.getExpirationTime());
        lqw.eq(StringUtils.isNotBlank(bo.getServerIp()), MediaServerConfig::getServerIp, bo.getServerIp());
        lqw.eq(StringUtils.isNotBlank(bo.getBtUrl()), MediaServerConfig::getBtUrl, bo.getBtUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getBtAccount()), MediaServerConfig::getBtAccount, bo.getBtAccount());
        lqw.eq(StringUtils.isNotBlank(bo.getBtPassword()), MediaServerConfig::getBtPassword, bo.getBtPassword());
        lqw.eq(StringUtils.isNotBlank(bo.getServerType()), MediaServerConfig::getServerType, bo.getServerType());
        lqw.eq(StringUtils.isNotBlank(bo.getServerPlatform()), MediaServerConfig::getServerPlatform, bo.getServerPlatform());
        return lqw;
    }

    /**
     * 新增服务器配置
     */
    @Override
    public Boolean insertByBo(MediaServerConfigBo bo) {
        MediaServerConfig add = BeanUtil.toBean(bo, MediaServerConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务器配置
     */
    @Override
    public Boolean updateByBo(MediaServerConfigBo bo) {
        MediaServerConfig update = BeanUtil.toBean(bo, MediaServerConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MediaServerConfig entity){
        //TODO 做一些数据校验,如唯一约束
        LoginUser loginUser =  LoginHelper.getLoginUser();
        entity.setUserId(loginUser.getUserId());
        entity.setDeptId(loginUser.getDeptId());
    }

    /**
     * 批量删除服务器配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
